/**
 * EduVerse Credentials - API路由聚合器
 * 
 * 本文件将所有API路由聚合到一个统一的前缀下，简化API访问。
 */

const express = require('express');

/**
 * 创建API路由实例
 * @param {Object} components 应用组件对象，包含walletApp、issuerApp和verifierApp
 * @returns {express.Router} 聚合了所有API路由的Router实例
 */
function createApiRouter(components) {
  const { walletApp, issuerApp, verifierApp } = components;
  
  // 创建主API路由器
  const apiRouter = express.Router();

  // 添加API版本和文档路由
  apiRouter.get('/', (req, res) => {
    res.json({
      name: 'EduVerse Credentials API',
      version: '0.1.0',
      endpoints: [
        '/api/wallet',
        '/api/issuer',
        '/api/verifier'
      ],
      documentation: '/api/docs'
    });
  });

  // 添加API健康检查路由
  apiRouter.get('/health', (req, res) => {
    const health = {
      status: 'ok',
      timestamp: new Date().toISOString(),
      components: {
        wallet: Boolean(walletApp),
        issuer: Boolean(issuerApp),
        verifier: Boolean(verifierApp)
      }
    };
    res.json(health);
  });

  // 添加API文档路由
  apiRouter.get('/docs', (req, res) => {
    res.json({
      message: 'API文档正在建设中，请稍后查看。',
      apiBase: '/api',
      walletApi: '/api/wallet',
      issuerApi: '/api/issuer',
      verifierApi: '/api/verifier'
    });
  });

  // 整合钱包API路由
  if (walletApp && walletApp.router) {
    apiRouter.use('/wallet', walletApp.router);
  }

  // 整合发行方API路由
  if (issuerApp && issuerApp.router) {
    apiRouter.use('/issuer', issuerApp.router);
  }

  // 整合验证方API路由
  if (verifierApp && verifierApp.router) {
    apiRouter.use('/verifier', verifierApp.router);
  }

  return apiRouter;
}

module.exports = createApiRouter; 